package com.chaowen.service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

import android.os.Handler;
import android.os.Message;

import com.chaowen.config.Config;
import com.chaowen.model.UserInfo;
import com.chaowen.util.SocketCreate;

public class LogOutTask extends Task {

	private Handler handler;
	private String userName;//学号
	private String password;//密码
	private int loginState;

	@Override
	public void onPostExecute(int result) {
		Message msg = handler.obtainMessage();
		switch (result) {
		default:
			msg.what = 12;
			UserInfo.isLogout = true;
			UserInfo.isLogin = false;
			UserInfo.isSign = false;
			Config.startNumbers = 0;
			handler.sendMessage(msg);
			break;
//		default:
//			msg.what = 2;
//			handler.sendMessage(msg);
//			break;
		}
	}

	@Override
	public void execute() {
		onPostExecute(2);
//		this.userName = UserInfo.userName;
//		this.password = UserInfo.password;
//		Thread t = new Thread(LogOutTask.this);
//		t.start();
	}

	@Override
	public void run() {
		Socket socket = null;
		try {
			socket = SocketCreate.createSocket();
			BufferedReader reader = SocketCreate.getReader(socket);
			PrintWriter writer = SocketCreate.getWriter(socket);
			writer.println("login," + userName + "," + password);
			String get = reader.readLine();
			loginState = Integer.parseInt(get);
			onPostExecute(loginState);
		} catch (Exception e) {
			try {
				if (socket != null)
					socket.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			onPostExecute(2);
			e.printStackTrace();
		}
	}

	@Override
	public void setHandler(Handler handler) {
		this.handler = handler;
	}
}
